/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sack.yahoo.ym;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import ymsg.network.Session;


public class UserWindow extends javax.swing.JFrame implements Runnable {
    String recipient;
     Session yahooMessengerSession;
     MySessionListener mySessionListener;
    
 private javax.swing.JTextArea historyarea;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea messagearea;
    private javax.swing.JButton sendbutton;
    private JLabel jLabel1;
    /** Creates new form NewJFrame */
    public UserWindow(String recipient, Session yahooMessengerSession,MySessionListener mySessionListener) {
        this.recipient=recipient;
        this.yahooMessengerSession=yahooMessengerSession;
        this.mySessionListener=mySessionListener;
        initComponents();
    }

    void updateMessagingPanel(String string) {
       historyarea.append(string);
    }

    void updateMessaginglabel(String string) {
        jLabel1.setText(string);
    }
    
   
    private void initComponents() {
         jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        historyarea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        messagearea = new javax.swing.JTextArea();
        sendbutton = new javax.swing.JButton();
         jLabel1 = new javax.swing.JLabel();
  
	
        historyarea.setColumns(20);
        historyarea.setRows(5);
        jScrollPane1.setViewportView(historyarea);
historyarea.setEditable(false);
        messagearea.setColumns(20);
        messagearea.setRows(5);
        jScrollPane2.setViewportView(messagearea);
jScrollPane1.setAutoscrolls(true);
jScrollPane2.setAutoscrolls(true);
        sendbutton.setText("Send");
        
        messagearea.addKeyListener(new java.awt.event.KeyListener() {

            public void keyTyped(KeyEvent e) {
             
            }

            public void keyPressed(KeyEvent e) {
               if(e.getKeyCode()==e.VK_ENTER)
                {
                    sendMessage(recipient,messagearea.getText());
               historyarea.append("Me : "+messagearea.getText()+"\n");
               messagearea.setText(""); 
              
                }
            }

            public void keyReleased(KeyEvent e) {
             
            }
        });
        
 sendbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendbuttonActionPerformed(evt);
            }

            private void sendbuttonActionPerformed(ActionEvent evt) {
               sendMessage(recipient,messagearea.getText());
                historyarea.append("Me : "+messagearea.getText()+"\n");
               messagearea.setText("");
         
          
            
            }
        });
          jLabel1.setText("");
       javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(sendbutton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(sendbutton, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

this.setVisible(true);
this.setSize(400,300);
this.setLocation(250,300);
       
   
        
    }
    
    
    
 public void sendMessage(String messageTo,String messageText) {

		System.out.println("Attempting to send message");
		
		try {
			yahooMessengerSession.sendMessage(messageTo, messageText);
			
		} catch (IllegalStateException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
 
 
 
    public void run() {
       
      
    }
       
}
